package mysql

import "gorm.io/gorm"

type UserWeek struct {
	gorm.Model
	Username string `gorm:"type:varchar(20);comment:账户;unique;"`
	Password string `gorm:"type:char(32);comment:密码;"`
	Sale     string `gorm:"type:char(5);comment:盐;"`
	Mobile   string `gorm:"type:char(18);comment:手机号;"`
	Avatar   string `gorm:"type:varchar(255);comment:头像;"`
}

// 根据用户名获取用户信息
func (u *UserWeek) GetByUsernameInfo(username string) error {
	return DB.Where("username = ?", username).Find(&u).Error
}

// 根据手机号获取用户信息
func (u *UserWeek) GetByMobileInfo(Mobile string) error {
	return DB.Where("mobile = ?", Mobile).Find(&u).Error
}

// 用户账户密码登录
func (u *UserWeek) LoginByUsernamePwd(username, password string) error {
	return DB.Where("username = ?", username).Where(
		"password = ?", password).Find(&u).Error
}

// 修改用户头像
func (u *UserWeek) UpdateAvatar(id int, avatar string) error {
	return DB.Model(&UserWeek{}).Where("id =?", id).Update("avatar", avatar).Error
}
